//
//  BCShopCarManager.m
//  BangcaiBuy
//
//  Created by lsy on 16/6/17.
//  Copyright © 2016年 BCB. All rights reserved.
//

#import "BCShopCarManager.h"
#import "AppDelegate.h"
@interface BCShopCarManager ()
{
    UIView *view;
    UILabel *choosedLab;
    UILabel *label1;
    UILabel *totalLab;
    UIButton *nextButt;
    UILabel *line;
    
    UIView *logoView;
    UILabel *logochoosedLab;
    UILabel *logolabel1;
    UILabel *logototalLab;
    UIButton *logonextButt;

    
    AppDelegate * app;
}
@end

@implementation BCShopCarManager
BCPLEMENT_SHARED_INSTANCE(BCShopCarManager)

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self createView];
        [self getData];

    }
    return self;
}

- (void)getData{
    NSArray *dataArr = [BCOrderViewManager readStuffFromOrder];
    [dataArr enumerateObjectsUsingBlock:^(ORDER* obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self addStuffName:obj.name amount:obj.count.integerValue price:obj.price.floatValue type:obj.category.integerValue];
    }];
}

- (void)setButTitle:(NSString *)butTitle
{
    _butTitle = butTitle;
    [nextButt setTitle:butTitle forState:UIControlStateNormal];
}

- (void)setStuffName:(NSString *)name amount:(NSInteger)amount price:(double)price type:(BCStuffType)type
{
    BOOL had = NO;
    if (self.stuffList.count == 0) {
        self.stuffList = [[NSMutableArray alloc] init];
    }
    self.amount += 1;
    for (BCShopModel *stuff in self.stuffList) {
        if ([stuff.name isEqualToString:name]) {
            self.totalPrice -= (stuff.amout * price);
            stuff.amout = amount;
            had = YES;
            self.amount -= 1;
        }
    }
    if (!had) {
        BCShopModel *stuff = [[BCShopModel alloc] init];
        stuff.name = name;
        stuff.amout = amount;
        stuff.price = price;
        stuff.type = type;
        [self.stuffList addObject:stuff];
        had = NO;
    }
    self.totalPrice +=  amount * price;
    
    NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:_stuffList.count];
    [_stuffList enumerateObjectsUsingBlock:^(BCShopModel *stuff, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([stuff.name isEqualToString:name]) {
            if (stuff.amout == 0) {
                [tempArray addObject:stuff];
                self.amount -=1;
            }
        }
    }];
    [_stuffList removeObjectsInArray:tempArray];
    
    totalLab.text = [NSString stringWithFormat:@"%ld", self.amount];
    choosedLab.text = [NSString stringWithFormat:@"¥ %.2f", self.totalPrice];
    
    logototalLab.text = [NSString stringWithFormat:@"%ld", self.amount];
    logochoosedLab.text = [NSString stringWithFormat:@"¥ %.2f", self.totalPrice];
}

- (void)addStuffName:(NSString *)name amount:(NSInteger)amount price:(double)price type:(BCStuffType)type
{
    BOOL had = NO;
    if (self.stuffList.count == 0) {
        self.stuffList = [[NSMutableArray alloc] init];
    }
    self.amount += 1;
    for (BCShopModel *stuff in self.stuffList) {
        if ([stuff.name isEqualToString:name]) {
            stuff.amout += amount;
            had = YES;
            self.amount -= 1;
        }
    }
    if (!had) {
        BCShopModel *stuff = [[BCShopModel alloc] init];
        stuff.name = name;
        stuff.amout = amount;
        stuff.price = price;
        stuff.type = type;
        [self.stuffList addObject:stuff];
        had = NO;
    }
    self.totalPrice +=  amount* price;

    totalLab.text = [NSString stringWithFormat:@"%ld", self.amount];
    choosedLab.text = [NSString stringWithFormat:@"¥ %.2f", self.totalPrice];
    
    logototalLab.text = [NSString stringWithFormat:@"%ld", self.amount];
    logochoosedLab.text = [NSString stringWithFormat:@"¥ %.2f", self.totalPrice];
}

- (void)removeStuffName:(NSString*)name amount: (NSInteger)amount price:(double)price type:(BCStuffType)type{
   
    NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:_stuffList.count];
    [_stuffList enumerateObjectsUsingBlock:^(BCShopModel *stuff, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([stuff.name isEqualToString:name]) {
            
            stuff.amout -= amount;
            if (stuff.amout == 0) {
                [tempArray addObject:stuff];
            }
            else {
                self.amount += 1;
            }
        }
    }];
    [_stuffList removeObjectsInArray:tempArray];
    self.totalPrice -= amount * price;
    self.amount -= 1;
    
    totalLab.text = [NSString stringWithFormat:@"%ld", self.amount];
    choosedLab.text = [NSString stringWithFormat:@"¥ %.2f", self.totalPrice];
    logototalLab.text = [NSString stringWithFormat:@"%ld", self.amount];
    logochoosedLab.text = [NSString stringWithFormat:@"¥ %.2f", self.totalPrice];
}

- (void)createView
{
    UIWindow *win = [[UIApplication sharedApplication].windows lastObject];
   
    view = [[UIView alloc] init];
    label1 = [[UILabel alloc] init];
    label1.font = [UIFont systemFontOfSize:18];
    choosedLab = [[UILabel alloc] init];
    totalLab = [[UILabel alloc] init];
    
    
    totalLab.backgroundColor = [UIColor colorWithRed:0.999  green:0.241  blue:0.245 alpha:1];
    totalLab.layer.cornerRadius = 10;
    totalLab.font = [UIFont systemFontOfSize:12];
    nextButt = [UIButton buttonWithType:UIButtonTypeSystem];
    [nextButt setTitle:@"下订单" forState:UIControlStateNormal];
    [nextButt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [nextButt addTarget:self action:@selector(nextStep) forControlEvents:UIControlEventTouchUpInside];
    label1.text = @"预估价:";
    label1.font = [UIFont systemFontOfSize:15];
    [nextButt setBackgroundColor:[UIColor colorWithRed:1  green:0.563  blue:0.044 alpha:1]];
     [win addSubview:view];
    line = [[UILabel alloc] init];
    line.backgroundColor = [UIColor colorWithRed:0.670  green:0.675  blue:0.674 alpha:1];
    choosedLab.textColor = [UIColor colorWithRed:1  green:0.314  blue:0 alpha:1];
    view.backgroundColor = [UIColor colorWithRed:0.904  green:0.974  blue:0.932 alpha:1];
   
    
    totalLab.layer.cornerRadius = 10;
    totalLab.layer.masksToBounds = YES;
    totalLab.textAlignment = NSTextAlignmentCenter;
    
    totalLab.textColor = [UIColor whiteColor];
  
    [view addSubview:_logoImg];
    [view addSubview:label1];
    [view addSubview:choosedLab];
    [view addSubview:totalLab];
    [view addSubview:nextButt];
    [view addSubview:line];
    
    
    logoView = [[UIView alloc] init];
    logolabel1 = [[UILabel alloc] init];
    logolabel1.font = [UIFont systemFontOfSize:18];
    logochoosedLab = [[UILabel alloc] init];
    logototalLab = [[UILabel alloc] init];
    
    
    logototalLab.backgroundColor = [UIColor colorWithRed:0.999  green:0.241  blue:0.245 alpha:1];
    logototalLab.layer.cornerRadius = 10;
    logototalLab.font = [UIFont systemFontOfSize:12];
    logonextButt = [UIButton buttonWithType:UIButtonTypeSystem];
    [logonextButt setTitle:@"下订单" forState:UIControlStateNormal];
    [logonextButt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [logonextButt addTarget:self action:@selector(nextStep) forControlEvents:UIControlEventTouchUpInside];
    logolabel1.text = @"预估价:";
    logolabel1.font = [UIFont systemFontOfSize:15];
    [logonextButt setBackgroundColor:[UIColor colorWithRed:1  green:0.563  blue:0.044 alpha:1]];
    [win addSubview:logoView];
    logochoosedLab.textColor = [UIColor colorWithRed:1  green:0.314  blue:0 alpha:1];
    logoView.backgroundColor = [UIColor colorWithRed:0.904  green:0.974  blue:0.932 alpha:1];
    
    
    logototalLab.layer.cornerRadius = 10;
    logototalLab.layer.masksToBounds = YES;
    logototalLab.textAlignment = NSTextAlignmentCenter;
    
    logototalLab.textColor = [UIColor whiteColor];
    
    _logoImg = [UIButton buttonWithType:UIButtonTypeCustom];
    [_logoImg setImage:[UIImage imageNamed:@"购物车"] forState:UIControlStateSelected];
    [_logoImg setImage:[UIImage imageNamed:@"购物车1"] forState:UIControlStateNormal];
    
    [_logoImg addTarget:self action:@selector(loTo:) forControlEvents:UIControlEventTouchUpInside];
    [logoView addSubview:_logoImg];
    [logoView addSubview:logolabel1];
    [logoView addSubview:logochoosedLab];
    [logoView addSubview:logototalLab];
    [logoView addSubview:logonextButt];
    view.hidden = YES;
    logoView.hidden = YES;
}

- (void)setAmount:(NSInteger)amount
{
    _amount = amount;
    if (_amount) {
        _logoImg.selected = YES;
    }else{
        _logoImg.selected = NO;
    }
}

- (void)loTo:(UIButton *)but{
    if (but.selected) {
        [self.delegate logoTo];

    }
}

- (void)layOutWithLogo{
    self.style = 0;
    self.hidden = NO;
    logoView.hidden = NO;
    view.hidden = YES;
    [_logoImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-12);
        make.bottom.mas_equalTo(-8);
        make.size.and.height.mas_equalTo(57);
        
    }];

    [logolabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.width.mas_equalTo(60 * wx);
        make.top.and.bottom.mas_equalTo(0);
    }];
    
    [logochoosedLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(logolabel1);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(49);
        make.left.mas_equalTo(logolabel1.mas_right);
        
    }];
    
    
    [logototalLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.and.height.mas_equalTo(20 * wx);
        make.bottom.mas_equalTo(-45);
        make.left.mas_equalTo(_logoImg.mas_left).with.offset(36);
    }];
    
    [logonextButt mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.and.right.and.bottom.mas_equalTo(0);
        make.width.mas_equalTo(110 * wx);
    }];
    
    logonextButt.hidden = YES;
    
    logototalLab.text = [NSString stringWithFormat:@"%ld", self.amount];
    logochoosedLab.text = [NSString stringWithFormat:@"¥ %.2f", self.totalPrice];
    logoView.frame = CGRectMake(0, SCREEN_HEIGHT - 49, SCREEN_WIDTH, 49);
    

}

- (void)showShopWindow{
    self.style = 1;
    self.hidden = NO;
    logoView.hidden = YES;
    view.hidden = NO;
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20 * wx);
        make.width.mas_equalTo(60 * wx);
        make.top.and.bottom.mas_equalTo(0);
    }];
    
    [choosedLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label1);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(49);
        make.left.mas_equalTo(label1.mas_right);
        
    }];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(choosedLab.mas_right);
        make.centerY.mas_equalTo(0);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(1);
        
    }];
    
    [totalLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(20 * wx);
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(choosedLab.mas_right).with.offset(30);
    
       
    }];
    
    [nextButt mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.and.right.and.bottom.mas_equalTo(0);
        make.width.mas_equalTo(110 * wx);
    }];

    totalLab.text = [NSString stringWithFormat:@"%ld", self.amount];
    choosedLab.text = [NSString stringWithFormat:@"¥ %.2f", self.totalPrice];
    view.frame = CGRectMake(0, SCREEN_HEIGHT - 49, SCREEN_WIDTH, 49);

   

}
- (void)showShopWindowUp{
    self.style = 2;
    self.hidden = NO;
    logoView.hidden = YES;
    view.hidden = NO;
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20 * wx);
        make.width.mas_equalTo(60 * wx);
        make.top.and.bottom.mas_equalTo(0);
    }];
    
    [choosedLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label1);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(49);
        make.left.mas_equalTo(label1.mas_right);
        
    }];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(choosedLab.mas_right);
        make.centerY.mas_equalTo(0);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(1);
        
    }];
    
    [totalLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(20 * wx);
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(choosedLab.mas_right).with.offset(30);
    }];
    
    [nextButt mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.and.right.and.bottom.mas_equalTo(0);
        make.width.mas_equalTo(110 * wx);
    }];
   

    totalLab.text = [NSString stringWithFormat:@"%ld", self.amount];
    choosedLab.text = [NSString stringWithFormat:@"¥ %.2f", self.totalPrice];
    view.frame = CGRectMake(0, SCREEN_HEIGHT - 98, SCREEN_WIDTH, 49);

}

- (void)setTotalPrice:(CGFloat)totalPrice
{
    if (totalPrice <=0) {
        totalPrice =0;
    }
    _totalPrice = totalPrice;

}

- (void)hideShopWindow{
    self.hidden = YES;
    view.hidden = YES;
    logoView.hidden = YES;
}
- (void)refreshWindow{
    
}

- (void)nextStep
{
    [self.delegate goTo];
}

- (void)clickAction{
    NSLog(@"click");
}

@end
